package com.example.myjsonspringbootstarter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xiaoxu.ma
 * @date 2021/11/22 20:48
 */
@Configuration
@EnableConfigurationProperties(MyJsonProperties.class)
@ConditionalOnClass(MyJsonService.class)
public class MyjsonAutoConfiguration {

    @Autowired
  private   MyJsonProperties myJsonProperties;

    @Bean
    @ConditionalOnProperty(prefix = MyJsonProperties.PREFIX, name = "enabled", matchIfMissing = true)
    @ConditionalOnMissingBean(MyJsonService.class)
    public  MyJsonService myJsonService(){
        MyJsonService myJsonService = new MyJsonService();
        myJsonService.setPrefixName(myJsonProperties.getPrefixName());
        myJsonService.setSuffixName(myJsonProperties.getSuffixName());
        return myJsonService;
    }
}
